package cn.ffcs.uec.view.component;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;

import cn.ffcs.uec.view.component.bean.UecStaffAllScListboxExtBean;
import cn.ffcs.uec.view.manager.LogDownManager;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ExportExcelNew;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 人员推荐信息汇总表(市场类)
 * 
 * @Title:
 * @Description:
 * @author BMP
 * @版权 FFCS（C） 2017
 * @date 2019年9月10日
 */
@Controller
@Scope("prototype")
public class UecStaffAllScListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = -885448835759987759L;

    private static final String STAFF_AND_OUTSTAFF_ZUL_SC = "/pages_nmg/views/uecstaffall/uecStaffAllSC_listbox_ext.zul";

    private UecStaffAllScListboxExtBean bean = new UecStaffAllScListboxExtBean();

    private UecStaffAll uecStaffAll = new UecStaffAll();

    private LogDownManager logDownManager = (LogDownManager) ApplicationContextUtil
            .getBean("logDownManager");

    List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    //为了区分申报专业，形成报表（市场类、营销类、综合类）
    private String type="市场类";

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecStaffAllScListboxExt() {
        Executions.createComponents(STAFF_AND_OUTSTAFF_ZUL_SC, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
    }

    /**
     * 界面初始化
     */
    public void onCreate() {
    	onQueryStaffTotalInfo();
    }

    /**
     * 查询员工信息
     * 
     * @throws Exception
     *             异常
     */
    public void onQueryStaffTotalInfo() {
        int activePage = bean.getUecStaffAllScListboxPaging().getActivePage();
        int pageSize = bean.getUecStaffAllScListboxPaging().getPageSize();
        PageInfo pageInfo = logDownManager.queryPageInfoByUecStaffAll(uecStaffAll,type, activePage + 1,
                pageSize);
        // 总条数
        if(pageInfo !=null ) {
	        bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
	        ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
	        if(list !=null && list.getSize()>0) {
	        	this.bean.getUecStaffAllScListboxs().setModel(list);
	        }
	        this.bean.getUecStaffAllScListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }

    /**
     * 每页显示事件
     */
    public void onPageSizeSelect() {
        if (this.bean.getPageScListbox() != null && this.bean.getPageScListbox().getSelectedItem().getValue() != null) {
            this.bean.getUecStaffAllScListboxPaging().setActivePage(0);
            this.bean.getUecStaffAllScListboxPaging()
                    .setPageSize(Integer.parseInt(this.bean.getPageScListbox().getSelectedItem().getValue().toString()));
        }
        this.onQueryStaffTotalInfo();
    }

    /**
     * 翻页
     */
    public void onStaffTotalInfoListboxPaging() {
        onQueryStaffTotalInfo();
    }
    
    /**
     * 查询按钮
     * @throws Exception
     * @throws InterruptedException
     */
    public void onReportParametr() throws Exception, InterruptedException {
    	uecStaffAll = new UecStaffAll();
    	uecStaffAll.setCode(!StrUtil.isNullOrEmpty(bean.getStaffCode().getValue())?bean.getStaffCode().getValue():""); 
        uecStaffAll.setName(!StrUtil.isNullOrEmpty(bean.getStaffName().getValue())?bean.getStaffName().getValue():"");
        this.onQueryStaffTotalInfo();
    }

    /**
     * 清单下载按钮
     * 
     * @throws Exception
     */
    public void onDownloadlist() throws Exception {
    	
    	  PageInfo pageInfo = logDownManager.queryPageInfoByUecStaffAll(uecStaffAll, type, 1,
                  20);
    	  if(pageInfo==null || pageInfo.getDataList().size()<=0) {
    		  Messagebox.show("清单数据为空");
    		  return;
    	  }
    	  // 清单数据
        data = logDownManager.getLogDownList(uecStaffAll,type);
        if (data !=null && data.size() != 0) {
            // 设置字符集
            String charset = "UTF-8";
            String fileName = "人员推荐汇总表(市场类)_" + DateUtil.getYYYYMMDD(new Date());
            HttpServletRequest httpRequest = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ExportExcelNew.exportExcel("", data, os);
            byte[] content = os.toByteArray();
            String encodedName = null;
            encodedName = URLEncoder.encode(fileName + ".xlsx", charset);
            // 将空格替换为+号
            encodedName = encodedName.replace("%20", "+");
            // 解决ie6 bug 或者是火狐浏览器
            if (encodedName.length() > 150 || Servlets.isGecko(httpRequest) || Servlets.isGecko3(httpRequest)) {
                encodedName = new String(encodedName.getBytes(charset), "ISO8859-1");
            }
            Filedownload.save(content, "application/octet-stream", encodedName);
        } else {
            Messagebox.show("数据为空!");
            return;
        }
    }

    /**
     * 重置按钮
     */
    public void onReset() {
        clearVariable();
        this.bean.getTotalSize().setValue(null);
        ListboxUtils.clearListbox(this.bean.getUecStaffAllScListboxs());
        this.bean.getUecStaffAllScListboxPaging().setTotalSize(1);
        uecStaffAll = new UecStaffAll();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecStaffAll = null;
        data.clear();
    }
}
